<?php
namespace app\api\controller;

use app\admin\model\Category;
use app\api\controller\BaseClientController;
use support\Redis;
use support\Request;

class CategoryController extends BaseClientController
{

    public $noNeedLogin = ['index'];

    public function index(Request $request)
    {
        // $pageNum   = $request->get('pageNum', 1);
        // $pageSize  = $request->get('pageSize', 10);
        // $paginator = Category::paginate([
        //     'page'      => $pageNum,
        //     'list_rows' => $pageSize,
        // ]);

        // $list  = $paginator->items();
        // $total = $paginator->total();

        // return $this->success([
        //     'list'  => $list,
        //     'total' => $total,
        // ]);

        // redis中取出所有
        $categoryListCash = Redis::get('category_list');
        $categoryList     = json_decode($categoryListCash, true);

        return $this->success($categoryList);
    }

    public function detail(Request $request)
    {
        $id = $request->get('id', 0);
        if (empty($id)) {
            return $this->error('参数错误');
        }
        $category = Category::find($id);
        if (empty($category)) {
            return $this->error('分类不存在');
        }
        return $this->success($category);
    }
}
